{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div class=\"contentcontainer med left\" style=\"margin-left: -50px;\">\n",
    "    <dl class=\"dl-horizontal\">\n",
    "      <dt>Title</dt> <dd> Bounds & selection stream example</dd>\n",
    "      <dt>Description</dt> <dd>A linked streams example demonstrating how to use Bounds and Selection streams together.</dd>\n",
    "      <dt>Backends</dt> <dd> Bokeh</dd>\n",
    "      <dt>Tags</dt> <dd> streams, linked, position, interactive</dd>\n",
    "    </dl>\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import holoviews as hv\n",
    "from holoviews import streams\n",
    "hv.extension('bokeh')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%opts Histogram {+framewise}\n",
    "\n",
    "# Declare distribution of Points\n",
    "points = hv.Points(np.random.multivariate_normal((0, 0), [[1, 0.1], [0.1, 1]], (1000,)))\n",
    "\n",
    "# Declare points selection selection\n",
    "sel = streams.Selection1D(source=points)\n",
    "\n",
    "# Declare DynamicMap computing mean y-value of selection\n",
    "mean_sel = hv.DynamicMap(lambda index: hv.HLine(points['y'][index].mean() if index else -10),\n",
    "                         kdims=[], streams=[sel])\n",
    "\n",
    "# Declare a Bounds stream and DynamicMap to get box_select geometry and draw it\n",
    "box = streams.Bounds(source=points, bounds=(0,0,0,0))\n",
    "bounds = hv.DynamicMap(lambda bounds: hv.Bounds(bounds), streams=[box])\n",
    "\n",
    "# Declare DynamicMap to apply bounds selection\n",
    "dmap = hv.DynamicMap(lambda bounds: points.select(x=(bounds[0], bounds[2]),\n",
    "                                                  y=(bounds[1], bounds[3])),\n",
    "                     streams=[box])\n",
    "\n",
    "# Compute histograms of selection along x-axis and y-axis\n",
    "yhist = hv.operation.histogram(dmap, bin_range=points.range('y'), dimension='y', dynamic=True, normed=False)\n",
    "xhist = hv.operation.histogram(dmap, bin_range=points.range('x'), dimension='x', dynamic=True, normed=False)\n",
    "\n",
    "# Combine components and display\n",
    "points * mean_sel * bounds << yhist << xhist"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<center><img src=\"http://assets.holoviews.org/gifs/examples/streams/bokeh/bounds_selection.gif\" width=400></center>"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
